package parser;

import antlr.RecognitionException;
import java.util.*;

class TryExcept extends Stmt {
    
    Stmt suite;
    Except except;
    Stmt handler;
    Stmt else_part;
    
    TryExcept(Stmt suite, Except except, Stmt handler) {
        this.suite = suite;
        this.except = except;
        this.handler = handler;
        yields = suite.yields | handler.yields;
        deletes = suite.deletes | handler.deletes;
    }
    
    void write(ByteStream out) throws RecognitionException {
        out.line(line);
        Patch patch = out.branch(Opcodes.PROTECT);
        suite.write(out);
        Patch patch2 = out.branch(Opcodes.END_PROTECT);
        out.patch(patch);
        except.write(out);
        handler.write(out);
        out.patch(patch2);
        if (else_part != null) {
            Patch end = out.branch(Opcodes.JUMP);
            else_part.write(out);
            out.patch(end);
        }
    }
        
    void addElse(Stmt suite) {
        else_part = suite;
        yields |= suite.yields;
        deletes |= suite.deletes;
    }
 
}
